<?php	
	class TagsController extends AppController{
		var $name = 'Tags';
		function index(){
			
		}
		function search($keyword = null ){
			$this->layout = null;
			$this->set('keyword',$keyword);
			if(!empty($keyword)){
				// find out the tags which contains keyword.
				$result = $this->Tag->find('all',array(
					'conditions'=>array('Tag.name like' =>'%'.$keyword.'%'),
					'fields'=>array('Tag.id','Tag.name'),
					'limit'=>5
				));
				// if the search result isn't null ,find the post according to the tags .
				if(!empty($result)){
					$this->loadModel('PostHasTag');
					$this->loadModel('Post');
					$this->loadModel('Response') ;
					$this->loadModel('User') ;
					
					$tags = null ;
					foreach ($result as $key => $value) {
						$tags[$key] = $value['Tag']['id'];
					}
					// find  the post posts_id of the posts which have the current tags.
					$tmp = $this->PostHasTag->find('all',array(
						'conditions'=>array('PostHasTag.tags_id'=>$tags),
						'fields'=>array('PostHasTag.posts_id'),
						'order'=>'PostHasTag.id desc',
						'limit'=>50
					));
					
					$postsID =  null ;
					foreach ($tmp as $key2 => $value2 ){
						$postsID[$key2] = $value2['PostHasTag']['posts_id'] ;
					}
					// select the posts according to the posts' ids .
					
					$posts = array();
					$posts = $this->Post->find('all',array(
							'conditions'=>array('Post.id' => $postsID),
							'fields' =>array('Post.id','Post.title','Post.excerpt','Post.reblog_posts_id','Post.users_id','Post.blogs_id','Post.response_count'),
							'order'=>'Post.post_time desc ',
					));
					// find the tags of every tags.
					$this->PostHasTag->unBindModel(array('belongsTo'=>array('Post')));
					foreach($posts as $key3 => $value ){
						// find the author'name of the post .that  is the userame of the user_id
						$tmp = $this->User->find('first',array(
							'conditions'=>array('User.id'=>$value['Post']['users_id']),
							'fields'=>array('User.username')
						));
						$posts[$key3]['Post']['username'] = $tmp['User']['username'] ;
		 				// set the blog 's icon .
		 				$posts[$key3]['Post']['blogs_icon'] = '' ;
		 				//find the post's tags.
			 			$tags = $this->PostHasTag->find('all',array(
			 				'conditions'=>array('PostHasTag.posts_id'=>$value['Post']['id']),
			 				'fields'=>array('Tag.id' ,'Tag.name')
			 			));
			 			$i = 0 ;
			 			foreach ($tags as $key4 => $value4 ){
			 				$posts[$key3]['Post']['tags'][$i]['name'] = $value4['Tag']['name'] ;
			 				$posts[$key3]['Post']['tags'][$i]['id']	 = $value4['Tag']['id'] ;
			 				++$i ;
			 			}
		 			}
					$this->set('posts',$posts);
				}
			}
		}
		function spotlight(){
			
		}
		function isTagExsit($tagName){
			$this->layout = null;
			$this->autoRender = false;
			
			$tag = $this->Tag->find('first',array(
				'conditions'=>array(
					'Tag.name'=>$tagName,
				 ),
				'fields'=>array('Tag.id')
				));
			if($tag['Tag']['id']){
				return true;
			}else{
				return false;
			}
		}
		function add($tagName){
			if(!$this->isTagExsit($tagName)){
				$this->Tag->save(array(
						'Tag'=>array(
							'name'=> $tagName
						)
				));
				//last insert id
				$last_insert_id = $this->Tag->id;
			}			
		}
	}
?>